//
//  Custom.m
//  Book
//
//  Created by zhiyou on 16-3-27.
//  Copyright (c) 2016年 陈凯. All rights reserved.
//

#import "Custom.h"
#import "MyTable.h"
#import "ASIFormDataRequest.h"
#import "Book.h"
#import "XMlParser.h"
#define baseurl @"http://localhost:8080/Book1/LoginAndRegister"
#define baseurl @"http://localhost:8080/Book1/Bookr"
@implementation Custom
//登录注册信息
+(void)registerOrLoginRequestWithCommand:(NSString *)command Name:(NSString *)name Psw:(NSString *)psw Nickname:(NSString *)nickname Email:(NSString *)email Comletion:(test)comletion{
NSURL * url = [NSURL URLWithString:baseurl];
__weak ASIFormDataRequest * request = [ASIFormDataRequest requestWithURL:url];
[request addPostValue:command forKey:@"command"];
[request addPostValue:name forKey:@"username"];
[request addPostValue:psw forKey:@"password"];
[request addPostValue:nickname forKey:@"nickname"];
[request addPostValue:email forKey:@"email"];

[request setCompletionBlock:^{
   
        NSString*str= [[NSString alloc]initWithData:request.responseData encoding:NSUTF8StringEncoding];
 
    comletion(str);

}];
[request startAsynchronous];
}

+(void)json{
NSURL*url=[NSURL URLWithString:@"http://localhost:8080/Book1/Book.xml"];
   ASIHTTPRequest*request=[ASIHTTPRequest requestWithURL:url];
   __block ASIHTTPRequest*Myrequest=request;
    request.delegate=self;
    
  
    
[request setCompletionBlock:^{
    NSURL * url = [NSURL URLWithString:baseurl];
    __weak ASIFormDataRequest * request = [ASIFormDataRequest requestWithURL:url];
    [request addPostValue:command forKey:@"command"];
    [request addPostValue:name forKey:@"username"];
    [request addPostValue:psw forKey:@"password"];
    [request addPostValue:nickname forKey:@"nickname"];
    [request addPostValue:email forKey:@"email"];
     NSMutableArray*array1= [MyTable selectAllBook];
   XMlParser* parser=[[XMlParser alloc]initWithParser:Myrequest.responseData];
    for(Book*book in parser.dataArray){
        NSString*str=[NSString stringWithFormat:@"%d",book.kind];
            if(array1.count==0){
                [MyTable Addbookname:book.bookname author:book.author kind:str imagename:book.ImageName Detail:book.detail booklink:book.link];
            }
            else{
                for(int i=0;i<array1.count;i++){
                Book*book1=array1[i];
               
            if([book1.bookname isEqualToString:book.bookname]&&book.bookname.length!=0){
                return ;
            }else{
            [MyTable Addbookname:book.bookname author:book.author kind:str imagename:book.ImageName Detail:book.detail booklink:book.link];
            }
            }
        }
    }
    
}];
    [request startAsynchronous];

}




//登录信息
//+(void)LoginWithName:(NSString *)name pwd:(NSString *)pwd command:(int)command commpletion:(commpletion)commpletion{
//  NSURL*url=[NSURL URLWithString:[NSString stringWithFormat:@"%@command=%d&username=%@&password=%@",baseurl,command,name,pwd]];
//    ASIHTTPRequest*request=[ASIHTTPRequest requestWithURL:url];
//       [request setCompletionBlock:^{
//        NSDictionary*dic=[NSJSONSerialization JSONObjectWithData:request.responseData options:NSJSONReadingAllowFragments error:nil];
//        int i=[[dic objectForKey:@"result"]intValue];
//        if(i==1){
//            
//            commpletion(@"登录成功");
//            [MyTable addUserWithName:name Pwd:pwd Access_token:[dic objectForKey:@"access_token"]];
//        }else{
//            commpletion([dic objectForKey:@"error"]);
//        }
//    }];
//    [request startAsynchronous];
//
//}
//提示信息
+(void)setMessage:(NSString*)message{
    UIAlertView*alert=[[UIAlertView alloc]initWithTitle:@"提示" message:message delegate:self cancelButtonTitle:@"知道了" otherButtonTitles:nil, nil];
    [alert show];
}
//书籍分类
+(NSMutableArray*)SetWithDataArray:(NSMutableArray *)dataArray{
    
    NSMutableArray*array1=[[NSMutableArray alloc]init];
    NSMutableArray*array2=[[NSMutableArray alloc]init];
    NSMutableArray*array3=[[NSMutableArray alloc]init];
    NSMutableArray*array4=[[NSMutableArray alloc]init];
    NSMutableArray*array5=[[NSMutableArray alloc]init];
    NSMutableArray*array6=[[NSMutableArray alloc]init];
    NSMutableArray*array7=[[NSMutableArray alloc]init];
    NSMutableArray*array8=[[NSMutableArray alloc]init];
    NSMutableArray*array=[[NSMutableArray alloc]initWithObjects:array1,array2,array3,array4,array5,array6,array7,array8, nil];
    for(Book*book in dataArray){
        [array[book.kind-1] addObject:book];
    }
    return array;
}

@end
